package com.bwt.top.bwt_ad_flutter.model;

import com.bwt.top.bwt_ad_flutter.config.BwtAdParamConfig;

import java.io.Serializable;

import io.flutter.plugin.common.MethodCall;

public class AdParams implements Serializable {
    private String appId;
    private String logType;
    private String wechatAppId;
    private String posId;
    private String adId;
    private String adLoaderId;
    private Double adWidth;
    private Double adHeight;
    private String adBackground;
    private String adLogo;

    public AdParams() {
    }

    private AdParams(String appId
            , String logType
            , String wechatAppId
            , String posId
            , String adId
            , String adLoaderId
            , Double adWidth
            , Double adHeight
            , String adLogo
            , String adBackground
    ) {
        this.appId = appId;
        this.logType = logType;
        this.wechatAppId = wechatAppId;
        this.posId = posId;
        this.adId = adId;
        this.adLoaderId = adLoaderId;
        this.adWidth = adWidth;
        this.adHeight = adHeight;
        this.adLogo = adLogo;
        this.adBackground = adBackground;
    }

    public static AdParams parse(MethodCall call) {
        return new AdParams(
                call.argument(BwtAdParamConfig.KEY_APP_ID),
                call.argument(BwtAdParamConfig.KEY_LOG_TYPE),
                call.argument(BwtAdParamConfig.KEY_WECHAT_APP_ID),
                call.argument(BwtAdParamConfig.KEY_POS_ID),
                call.argument(BwtAdParamConfig.KEY_AD_ID),
                call.argument(BwtAdParamConfig.KEY_AD_LOADER_ID),
                call.argument(BwtAdParamConfig.KEY_AD_WIDTH),
                call.argument(BwtAdParamConfig.KEY_AD_HEIGHT),
                call.argument(BwtAdParamConfig.KEY_AD_LOGO),
                call.argument(BwtAdParamConfig.KEY_AD_BACKGROUND)
        );
    }

    public void setPosId(String posId) {
        this.posId = posId;
    }

    public void setAdId(String adId) {
        this.adId = adId;
    }

    public void setAdLoaderId(String adLoaderId) {
        this.adLoaderId = adLoaderId;
    }

    public void setAdWidth(Double adWidth) {
        this.adWidth = adWidth;
    }

    public void setAdHeight(Double adHeight) {
        this.adHeight = adHeight;
    }

    public void setAdBackground(String adBackground) {
        this.adBackground = adBackground;
    }

    public void setAdLogo(String adLogo) {
        this.adLogo = adLogo;
    }

    public String getPosId() {
        return posId;
    }

    public String getAdId() {
        return adId;
    }

    public String getAdLoaderId() {
        return adLoaderId;
    }

    public Double getAdWidth() {
        return adWidth;
    }

    public Double getAdHeight() {
        return adHeight;
    }

    public String getAdLogo() {
        return getImageName(adLogo);
    }

    public String getAdBackground() {
        return getImageName(adBackground);
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getLogType() {
        return logType;
    }

    public void setLogType(String logType) {
        this.logType = logType;
    }

    public String getWechatAppId() {
        return wechatAppId;
    }

    public void setWechatAppId(String wechatAppId) {
        this.wechatAppId = wechatAppId;
    }

    public String getString() {
        StringBuilder sb = new StringBuilder("{\n");
        if (appId != null) {
            sb.append("\t").append("appId: ").append(appId).append(", ").append("\n");
        }
        if (logType != null) {
            sb.append("\t").append("logType: ").append(logType).append(", ").append("\n");
        }
        if (wechatAppId != null) {
            sb.append("\t").append("wechatAppId: ").append(wechatAppId).append(", ").append("\n");
        }
        if (posId != null) {
            sb.append("\t").append("posId: ").append(posId).append(", ").append("\n");
        }
        if (adId != null) {
            sb.append("\t").append("adId: ").append(adId).append(", ").append("\n");
        }
        if (adLoaderId != null) {
            sb.append("\t").append("adLoaderId: ").append(adLoaderId).append(", ").append("\n");
        }
        if (adWidth != null) {
            sb.append("\t").append("adWidth: ").append(adWidth).append(", ").append("\n");
        }
        if (adHeight != null) {
            sb.append("\t").append("adHeight: ").append(adHeight).append(", ").append("\n");
        }
        if (adLogo != null) {
            sb.append("\t").append("adLogo: ").append(adLogo).append(", ").append("\n");
        }
        if (adBackground != null) {
            sb.append("\t").append("adBackground: ").append(adBackground).append(", ").append("\n");
        }
        sb.append("}");
        return sb.toString();
    }

    private String getImageName(String text) {
        if (text == null) return null;
        if (text.contains(".")) {
            int lastIndexOf = text.lastIndexOf(".");
            return text.substring(0, lastIndexOf);
        }
        return text;
    }
}
